package net.hserver.admin.system.gen;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import net.hserver.admin.system.bean.TableColumns;
import net.hserver.admin.system.bean.TableInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class Gen {

    private static String capitalizeFirstLetter(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }

    public static String genEntity(TableInfo tableInfo, List<TableColumns> tableColumns) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            data.put("tableColumns", tableColumns);
            return CodeTemplate.getTemplate("/entity.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String getMapper(TableInfo tableInfo) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            return CodeTemplate.getTemplate("/mapper.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String getService(TableInfo tableInfo,List<TableColumns> tableColumns) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            //获取字符串的字段
            Set<String> collect = tableColumns.stream().filter(k -> k.getJavaType().equals("String")).map(k -> "get" + capitalizeFirstLetter(k.getJavaName())).collect(Collectors.toSet());
            data.put("tableColumns", collect);
            //获取主键类型
            TableColumns pri = tableColumns.stream().filter(k -> k.getKey().equals("PRI")).findFirst().orElse(null);
            if (pri != null) {
                data.put("priType", pri.getJavaType());
                data.put("priName", "get"+capitalizeFirstLetter(pri.getJavaName()));
            }else {
                data.put("priType","String");
                data.put("priName","getId");

            }
            return CodeTemplate.getTemplate("/service.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String getController(TableInfo tableInfo,List<TableColumns> tableColumns) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            data.put("controllerName",StrUtil.toCamelCase(tableInfo.getTableName()));
            //获取主键类型
            TableColumns pri = tableColumns.stream().filter(k -> k.getKey().equals("PRI")).findFirst().orElse(null);
            if (pri != null) {
                data.put("priType", pri.getJavaType());
            }else {
                data.put("priType","String");
            }
            return CodeTemplate.getTemplate("/controller.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }


    public static String genTypes(TableInfo tableInfo, List<TableColumns> tableColumns) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            data.put("tableColumns", tableColumns);
            return CodeTemplate.getTemplate("/types.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String genApi(TableInfo tableInfo) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            data.put("controllerName",StrUtil.toCamelCase(tableInfo.getTableName()));
            return CodeTemplate.getTemplate("/api.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String genPage(TableInfo tableInfo,List<TableColumns> tableColumns) {
        try {
            String javaClassName = StrUtil.toCamelCase(tableInfo.getTableName());
            javaClassName=capitalizeFirstLetter(javaClassName);
            HashMap<String, Object> data = new HashMap<>();
            data.put("tableInfo", tableInfo);
            data.put("name",javaClassName);
            data.put("tableColumns",tableColumns);
            return CodeTemplate.getTemplate("/page.ftl", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

}